package ChessBoard;

import java.util.ArrayList;


public class Knight extends Figure {
	

	public Knight(boolean isWhite, int[] position,ChessBoard board) {
		super(isWhite, position, board);
		super.value = 3;
	}

	@Override
	protected ArrayList<int[]> calculatePos() {
		super.resetAll();
		ArrayList<int[]> newMoves = new ArrayList<int[]>();
		int x = super.position[0] + 1, y = super.position[1] + 2;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}
		x = super.position[0] - 1;
		y = super.position[1] + 2;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}
		x = super.position[0] + 1;
		y = super.position[1] - 2;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}
		x = super.position[0] - 1;
		y = super.position[1] - 2;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}
		x = super.position[0] + 2;
		y = super.position[1] + 1;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}
		x = super.position[0] + 2;
		y = super.position[1] - 1;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}
		x = super.position[0] - 2;
		y = super.position[1] + 1;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}
		x = super.position[0] - 2;
		y = super.position[1] - 1;
		if (x > 0 && x < 9 && y > 0 && y < 9) {
			if (!super.correspondingBoard.fieldIsEmpty(new int[] { x, y })) {
				helper(x, y);
			} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					newMoves.add(new int[] { x, y });
			}
		}

		return newMoves;
	}

	private void helper(int x, int y) {
		Figure f = super.correspondingBoard.getFigure(new int[] { x, y });
		if (super.isWhite == f.isWhite) {
			if (f.value != 100)
				super.covers.add(f);
		} else {
				if (super.correspondingBoard.moveProvokesNoCheck(x, y,this))
					super.kills.add(f);				
		}
	}

	@Override
	public void checkCheck() {
		if(this.kills.contains(super.correspondingBoard.getKing(!this.isWhite)))
			this.checks=true;
		else
			this.checks=false;
		
	}

}
